const koa = require("koa");
const app = new koa();
const router = require("koa-router")();
const static = require("koa-static");
const koaBody = require("koa-Body");
const path = require("path");
const fs = require("fs")
router.get("/",async ctx=>{
    ctx.body="连接成功"
})
router.post("/file",async ctx=>{
    // name age
    console.log(ctx.request.body);
     //创建一个可读流
    console.log(ctx.request.files.logo.path);
    var logo = ctx.request.files.logo.path;
    var reader = fs.createReadStream(logo);
    //获取上传图片所在的路径和扩展名
    console.log(path.basename(logo));
    var uploadFile = `./static/${path.basename(logo)}`;
    //创建一个可写流
    var writer = fs.createWriteStream(uploadFile);
    //4将可读写流通过管道写入可写流
    reader.pipe(writer);
   
    ctx.body = "文件上传成功"
})
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 200 * 1024 * 1024,    //设置默认上传文件的大小
           // 保留文件扩展名
		keepExtensions: true
    }
}))
app.use(router.routes());
app.use(static(path.join(__dirname,"static")))

app.listen(4006)
